﻿Imports CoreClassLibraryCMS
Imports System.Text

Public Class CreateTransferInput
    Inherits JSonDataCrud


    Public Overrides Function DeleteData(param As System.Collections.Specialized.NameValueCollection) As System.Web.Mvc.JsonResult

    End Function

    Public Overrides Function ReadData(Skip As Integer, Take As Integer, SortDir As String, Sort As String, param As String) As System.Web.Mvc.JsonResult

    End Function

    Private Function GetId(ByVal TahunREKAP As Integer) As String
        Dim strSql As String
        Dim Rec As New ADODBU.Recordset

        strSql = " Select top 1 TRFHTRANSCOD  from TTRFHD "
        strSql += " order by TRFHTRANSCOD desc"
        Rec.Open(strSql, "TTRFHD", Connection.GetDBConnData)
        If Not Rec.Eof Then
            If Rec.RecordCount = 0 Then
                GetId = "1"

            Else
                '    GetId = PADL(Val(Right(Rec.Fields("TRFHTRANSCOD"), 4)) + 1, 4, "0")
                GetId = Rec.Fields("TRFHTRANSCOD")
            End If
        Else
            GetId = "1"

        End If
        Return GetId
    End Function
    Private Function GetXRow(ByVal param As System.Collections.Specialized.NameValueCollection) As clsTTRFHDRRow
        Dim xRow As New clsTTRFHDRRow()

        If param.Item("TRFHTRANSCOD").ToString.Length = 0 Then
            xRow.TRFHTRANSCOD = GetId(Year(param.Item("TRFHTRANSCOD").ToString))
        Else
            xRow.TRFHTRANSCOD = param.Item("TRFHTRANSCOD").ToString
        End If

        xRow.TRFHSITEFROM = param.Item("FROM_STORE").ToString
        xRow.TRFHSITETO = param.Item("TO_STORE").ToString
        xRow.TRFHTYPE = param.Item("TYPE").ToString

       
        GetXRow = xRow
    End Function
    Public Overrides Function SaveData(param As System.Collections.Specialized.NameValueCollection) As System.Web.Mvc.JsonResult
        Dim getparam As String
        Dim strtype As String ' 1 = create 2= picking 3 = receiving

        getparam = "testing"
        Dim x As New clsTTRFHDR
        Dim xRow As New clsTTRFHDRRow()
        Dim y As New Web.Mvc.JsonResult

        Dim arr As New ArrayList
        Dim lContinue As Boolean = True
        Dim lResult As Boolean = False
        Dim mError As New StringBuilder
        Dim mLocalConn As New ADODBU.dbConn
        Dim StrKriteria As String
        ' Dim xrowSP3D As New clsACC_SP3DRow
        'ADODBU.RowClassUtil.RowClassFill(xRow, param)
        x.DbConn = Connection.GetDBConn

        mLocalConn = Connection.GetDBConn
        mLocalConn.Open()
        mLocalConn.CreateTransaction()
        If param.Item("TRFHTRANSCOD").ToString.Length = 0 Then
            xRow.TRFHTRANSCOD = GetId(Year(param.Item("TRFHTRANSCOD").ToString))
        Else
            xRow.TRFHTRANSCOD = param.Item("TRFHTRANSCOD").ToString
        End If

        xRow.TRFHSITEFROM = param.Item("FROM_STORE").ToString
        xRow.TRFHSITETO = param.Item("TO_STORE").ToString
        xRow.TRFHTYPE = param.Item("TYPE").ToString
        xRow.TRFHPROGCRE = Now.Date


        ' xRow = GetXRow(param)
        'x.Open(" where NO_RKP_SP3D ='" & xRow.NO_RKP_SP3D & "'")
        x.Open(" where TRFHTRANSCOD ='" & xRow.TRFHTRANSCOD & "' ")
        If x.ErrMessage.Length = 0 Then
            If x.RecordCount = 0 Then
                x.SaveClassRow(xRow, True, False)
                If x.ErrMessage.Length = 0 Then
                    arr = GetListDetail(param, xRow)
                    If Not arr Is Nothing Then
                        If arr.Count > 0 Then
                            For Each dtlRow As clsLTRFDETRow In arr
                                Dim dtl As New clsLTRFDET
                                dtl.DbConn = Connection.GetDBConnData
                                StrKriteria = " where TRFHTRANSCOD='" & xRow.TRFHTRANSCOD & "' "
                                StrKriteria += " and TRFLLINE='" & dtlRow.TRFLLINE & "' "
                                dtl.Open(StrKriteria)
                                If dtl.ErrMessage.Length = 0 Then
                                    If dtl.RecordCount = 0 Then
                                        dtl.SaveClassRow(dtlRow, True, False)
                                        If dtl.ErrMessage.Length = 0 Then
                                           
                                            lResult = True
                                        Else
                                            lResult = False
                                            mLocalConn.Transaction.Rollback()
                                        End If
                                    Else
                                        dtl.SaveClassRow(dtlRow, False, True)
                                        If dtl.ErrMessage.Length = 0 Then
                                            lResult = True
                                        Else
                                            lResult = False
                                            mLocalConn.Transaction.Rollback()
                                        End If
                                    End If
                                End If
                            Next
                        End If
                    End If
                Else
                    lResult = False
                    mError.Append(x.ErrMessage & vbCrLf)
                    mLocalConn.Transaction.Rollback()
                End If
            End If
        Else
            lResult = False
            mError.Append(x.ErrMessage & vbCrLf)
            mLocalConn.Transaction.Rollback()
        End If

        If lContinue = True Then
            If lResult = True Then
                mLocalConn.Transaction.Commit()
                Dim res = New With {.success = True, .ID_SETUP = xRow.TRFHTRANSCOD}
                y.Data = res
            Else
                Dim res = New With {.success = False, .pesan = mError.ToString}
                y.Data = res
            End If
        End If

        Return y
    End Function

    Public Overrides Function UpdateData(param As System.Collections.Specialized.NameValueCollection) As System.Web.Mvc.JsonResult

    End Function
    Private Function GetListDetail(ByVal param As System.Collections.Specialized.NameValueCollection, ByVal Tr As clsTTRFHDRRow) As ArrayList
        Dim arrList As New ArrayList
        Dim arrListRow As New ArrayList
        Dim i As Integer
        Dim jml As Integer = CInt(param.Item("JmlField"))
        Dim arr As New ArrayList

        arrList = clsSplitCriteria.splitListDetail(param.Item("List"), CInt(param.Item("JmlList")), jml)

        If Not arrList Is Nothing Then
            If arrList.Count > 0 Then
                For Each str As String() In arrList
                    If Not str Is Nothing Then

                        Dim TrDet As New clsLTRFDETRow ' clsACC_RKP_SP3D_DETRow
                        Dim arrListField As New ArrayList

                        For i = 0 To jml - 1
                            Dim splitField As String()
                            splitField = Split(str(i), "=", 2)
                            arrListField.Add(splitField(1))
                        Next

                        If arrListField.Item(0) = "" Then
                            Dim strSql As String
                            Dim rec As New ADODBU.Recordset
                            strSql = " Select top 1 TRFLLINE  from LTRFDET "
                            strSql += " order by TRFLLINE desc"
                            Rec.Open(strSql, "TRFLLINE", Connection.GetDBConnData)
                            If Not Rec.Eof Then
                                If Rec.RecordCount = 0 Then
                                    TrDet.TRFLLINE = 1

                                Else
                                    '    GetId = PADL(Val(Right(Rec.Fields("TRFHTRANSCOD"), 4)) + 1, 4, "0")
                                    TrDet.TRFLLINE = rec.Fields("TRFLLINE")
                                End If
                            Else
                                TrDet.TRFLLINE = 1

                            End If
                        Else
                            TrDet.TRFLLINE = arrListField.Item(0)
                        End If


                        TrDet.BARCODE = arrListField.Item(2) 'Tr.TGL_RKP_SP3D

                        TrDet.TRFLCOD = Tr.TRFHTRANSCOD
                        TrDet.TRFLCREQTY = arrListField.Item(3)
                      

                        arrListRow.Add(TrDet)
                    End If
                Next
            End If
        End If

        Return arrListRow
    End Function
End Class
